package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.domain.AdminSmMessage;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmMessageMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @项目名称：yusp-admin
 * @类名称：AdminSmLookupItemService
 * @类描述：数据字典内容
 * @功能描述:
 * @创建人：liaoxd@yusys.com.cn @创建时间：2017-12-13 11:18 @修改备注：
 * @修改日期 修改人员 修改原因 -------- -------- ----------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmMessageService extends CommonService {
	@Autowired
	private AdminSmMessageMapper mapper;

	@Override
	protected CommonMapper<AdminSmMessage> getMapper() {
		return this.mapper;
	}

	@CachePut(value = "AdminSmMessage", key = "#adminSmMessage.code")
	public AdminSmMessage createWithCache(AdminSmMessage adminSmMessage) {
		// 不做保存，只新增REDIS数据
//		List<AdminSmMessage> list = this.mapper.select(adminSmMessage);
//		AdminSmMessage message = null;
//		if(list !=null && list.size()> 0 ) {
//			message = this.mapper.select(adminSmMessage).get(0);
//		}
		return adminSmMessage;
	}

	@Transactional
	@CachePut(value = "AdminSmMessage", key = "#adminSmMessage.code")
	public AdminSmMessage updateWithCache(AdminSmMessage adminSmMessage) {
		
		adminSmMessage.setLastChgDt(DateUtil.formatDateTimeByDef());
		this.updateSelective(getMapper(), adminSmMessage);
		return adminSmMessage;
	}

	@Transactional
	@CacheEvict(value = "AdminSmMessage", key = "#code")
	public int deleteWithCache(String code) {
		AdminSmMessage item = new AdminSmMessage();
		item.setCode(code);
		return super.delete(item);
	}

	/**
     * @方法名称:getMessageInfoWithCache
     * @方法描述:根据code，查询消息列表，并加入缓存中
     * @参数与返回说明:
     * @算法描述:
     */
    @Cacheable(value = "AdminSmMessage", key = "#code")
    public AdminSmMessage getMessageInfoWithCache(String code) {
        AdminSmMessage adminSmMessageParm = new AdminSmMessage();
        adminSmMessageParm.setCode(code);
        
        List<AdminSmMessage> list = this.mapper.select(adminSmMessageParm);
            
        return (AdminSmMessage)list.get(0);
    }
    
	/**
	 * @方法名称:getMessageInfo
	 * @方法描述:根据code，查询消息列表，并加入缓存中
	 * @参数与返回说明:
	 * @算法描述:
	 */
	@Cacheable(value = "AdminSmMessage", key = "#code")
	public List<Map<String, String>> getMessageInfo(String code) {
		List<String> param = new ArrayList<String>();
		param.add(code);
		return this.mapper.getCodeList(param);
	}

	@Override
	public int insert(Object record) {
		AdminSmMessage adminSmMessage = (AdminSmMessage) record;
		adminSmMessage.setLastChgDt(DateUtil.formatDateTimeByDef());
		
		Map<String,String> map = new HashMap<String,String>();
		map.put("code", adminSmMessage.getCode());
		List<?> list = this.mapper.getMessageByCode(map);
		if(list != null && list.size() > 0 ) {
			return 0 ;
		}else {
			return this.insert(getMapper(), record);
		}
		
	}
	
	@Override
	public int updateSelective(Object record) {
		AdminSmMessage adminSmMessage = (AdminSmMessage) record;
		adminSmMessage.setLastChgDt(DateUtil.formatDateTimeByDef());
		return this.updateSelective(getMapper(), record);
	}
	
	/**
     * @函数名称:selectByModel
     * @函数描述:重新，支持页面模糊查询
     * @参数与返回说明:
     * @算法描述:
     */
    @Override
    @SuppressWarnings("unchecked")
	public <T> List<T> selectByModel(QueryModel model) {
		if (model.getCondition().containsKey("code")) {
			model.getCondition().put("code", "%" + model.getCondition().get("code") + "%");
		}
		if (model.getCondition().containsKey("message")) {
			model.getCondition().put("message", "%" + model.getCondition().get("message") + "%");
		}
		
		// 设置分页查询参数(设置到线程变量中了)
		PageHelper.startPage(model.getPage(), model.getSize());
		List<Map<String,String>> list=this.mapper.getMessageListBycodeOrName(model);
		// 清除线程绑定的分页参数，防止影响同线程的其他查询
		PageHelper.clearPage();
		
		return (List<T>) list;
	}


}
